<?php
namespace App\Service\Admin;

class WhywService extends BaseService
{
    //工时录入业务

    //数据列表
    public static function index($pages, $pas = [])
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = self::dbConn(self::$dbYwWh)
            ->where('del', 0);
        // if ($pas['uid'] && $pas['role_id'] == 4) {
        //     $db = $db->where('staff_id', $pas['uid']);
        // }
        // if (isset($pas['depart_id']) && $pas['depart_id']) {
        //     $db = $db->where('depart_id', $pas['depart_id']);
        // }
        $db = $db->where('staff_id', $pas['uid']);
        // if ($pas['sort_id'] == 2) {
        //     $db = $db->orderBy('in_day', 'desc');
        // } else {
        //     $db = $db->orderBy('in_day', 'asc');
        // }
        $total = $db->count();
        if ($pas['sort_id']) {
            $sort = $pas['sort_id']==2 ? 'desc' : 'asc';
            $db = $db->orderBy('in_day', $sort);
        }
        $res = $db->skip($start)->take($pages['limit'])->get();
        if (!count($res)) {
            return self::toback(-2, '数据不存在');
        }
        $types = self::$whStatuss;
        $arys = [];
        foreach ($res as $v) {
            $itemOne = (array) self::dbConn(self::$dbConfigItem)
                ->where('id', $v->item_id)
                ->first();
            $item_name = empty($itemOne) ? '-' : $itemOne['title'];
            $whOnes = self::dbConn(self::$dbYwWhItem)
                ->where('del', 0)
                ->where('wh_id', $v->id)
                ->orderBy('starthour', 'asc')
                ->get();
            $wh_str = '';
            $delay = 0;
            if (count($whOnes)) {
                foreach ($whOnes as $vv) {
                    $cfgWhOne = (array) self::dbConn(self::$dbConfigWorkHour)
                        ->where('id', $vv->config_whid)
                        ->first();
                    $wh_str .= empty($cfgWhOne) ? '' : $cfgWhOne['title'] . '：';
                    // $wh_str .= explode(' ', $vv->uptime)[0] . '，';
                    $wh_str .= $vv->starthour . '-' . $vv->endhour;
                    $wh_str .= '，时长' . $vv->delay . '分钟';
                    $wh_str .= '<br>';
                    $delay += $vv->delay;
                }
            }
            if ($delay) {
                $hour = floor($delay / 60);
                $fen = $delay - ($hour * 60);
                $delay_str = $hour . ' 小时 ' . $fen . ' 分钟';
            } else {
                $delay_str = '0 小时';
            }
            $taskOne = (array) self::dbConn(self::$dbYwTask)
                ->where('id', $v->task_id)
                ->first();
            $task_name = empty($taskOne) ? '-' : $taskOne['title'];
            $staffOne = (array) self::dbConn(self::$dbStaff)
                ->where('id', $v->staff_id)
                ->first();
            $staff_name = empty($staffOne) ? '-' : $staffOne['title'];
            $statusOne = (array) self::dbConn(self::$dbYwWhStatus)
                ->where('wh_id', $v->id)
                ->orderBy('id', 'desc')
                ->first();
            $status_id = empty($statusOne) ? '-' : $statusOne['type'];
            $status_name = array_key_exists($status_id, $types) ? $types[$status_id] : '-';
            $is_pub = '存草稿'; 
            $refuse_msg = '';
            if ($status_id == 2) { $is_pub = '已保存'; }
            if ($status_id == 3) { $is_pub = '已提交'; }
            if ($status_id == 4) { 
                $is_pub = '被拒绝，原因如下：'; 
                $refuse_msg = (!empty($statusOne)&&$statusOne['remark']) ? $statusOne['remark'] : '';
            }
            if ($status_id > 4) { $is_pub = '已发布'; }
            $arys[] = [
                'id' => $v->id,
                'item_id' => $v->item_id, 'item_name' => $item_name,
                'task_id' => $v->task_id, 'task_name' => $task_name,
                'wh_str' => $wh_str,
                'staff' => $v->staff_id, 'staff_name' => $staff_name,
                'status_id' => $status_id, 'status_name' => $status_name,
                'is_pub' => $is_pub, 'refuse_msg' => $refuse_msg,
                'lasttime' => $v->uptime, 'in_day' => $v->in_day,
                'delay' => $delay, 'delay_str' => $delay_str,
            ];
        }
        self::clearData(); //清理数据
        return self::toback(0, '', $arys, $total);
    }

    //详情
    public static function getOne($id, $pas)
    {
        $res = (array) self::dbConn(self::$dbYwWh)
            ->where('id', $id)
            ->first();
        if (!empty($res)) {
            $taskOne = (array) self::dbConn(self::$dbYwTask)
                ->where('id', $res['task_id'])
                ->first();
            $res['task_name'] = empty($taskOne) ? '' : $taskOne['title'];
        }
        $arys['data'] = $res;
        $itemOnes = self::dbConn(self::$dbYwWhItem)
            ->where('del', 0)
            ->orderBy('starthour', 'asc')
            ->groupBy('config_whid')
            ->get();
        $whList = [];
        $delay_total = 0;
        if (count($itemOnes) && $res) {
            foreach ($itemOnes as $v) {
                $configOne = (array) self::dbConn(self::$dbConfigWorkHour)
                    ->where('id', $v->config_whid)
                    ->first();
                $title = empty($configOne) ? '' : $configOne['title'];
                $subOnes = self::dbConn(self::$dbYwWhItem)
                    ->where('del', 0)
                    ->where('config_whid', $v->config_whid)
                    ->where('wh_id', $id)
                    ->orderBy('starthour', 'asc')
                    ->get();
                $subList = [];
                $delay_pid = 0;
                if (count($subOnes)) {
                    foreach ($subOnes as $vv) {
                        $cntOne = (array) self::dbConn(self::$dbConfigWhCnt)
                            ->where('id', $vv->cnt_id)
                            ->first();
                        $resDelay = self::getDelay($vv->starthour, $vv->endhour);
                        $subList[] = [
                            'id' => $vv->id,
                            //'title' => $vv->title,
                            'cnt_id' => $v->cnt_id,
                            'cnt_str' => empty($cntOne) ? '' : $cntOne['cnt'],
                            'starthour' => $vv->starthour,
                            'endhour' => $vv->endhour,
                            'delay' => $vv->delay,
                            'delay_str' => $resDelay['code'] < 0 ? '' : $resDelay['data']['str'],
                        ];
                        $delay_pid += $vv->delay;
                    }
                }
                $whList[] = [
                    'id' => $v->config_whid, 'title' => $title,
                    'config_whid' => $v->config_whid,
                    'config_title' => $title,
                    'child' => $subList,
                    'delay' => self::shiByFen($delay_pid),
                ];
                $delay_total += $delay_pid;
            }
        }
        $arys['data']['whList'] = $whList;
        $arys['data']['delay'] = self::shiByFen($delay_total);
        //录入日期
        $in_day = isset($arys['data']['in_day'])&&$arys['data']['in_day'] ? $arys['data']['in_day'] : date('Y-m-d', time());
        $arys['data']['in_day'] = $in_day;
        //项目
//        $arys['item'] = self::dbConn(self::$dbConfigItem)
//            ->select('id', 'title')
//            ->where('del', 0)
//            ->where('depart_id', $pas['depart_id'])
//            ->get();
        $arys['item'] = self::getItemListByUid($pas['uid'], $pas['depart_id']);
        //任务
        $arys['task'] = [];
        $taskOnes = self::dbConn(self::$dbYwTask)
            // ->select('id', 'title')
            ->where('del', 0)
            ->where('depart_id', $pas['depart_id'])
            ->where('in_day', $in_day)
            ->get();
        if (count($taskOnes)) {
            foreach ($taskOnes as $v) {
                $departOne = (array) self::dbConn(self::$dbStDepart)
                    ->where('id', $v->depart_id)
                    ->first();
                $depart_name = empty($departOne) ? '' : $departOne['title'];
                $arys['task'][] = [
                    'id' => $v->id, 'title' => $v->title,
                    'in_day' => $v->in_day, 'depart_name' => $depart_name,
                ];
            }
        }
        //默认项目
        $whUidOne = (array) self::dbConn(self::$dbConfigWhUid)
            ->where('del', 0)
            ->where('uid', $pas['uid'])
            ->first();
        $arys['data']['item_default'] = empty($whUidOne) ? 0 : $whUidOne['item_id'];
        $arys['data']['item_id'] = (isset($arys['data']['item_id'])&&$arys['data']['item_id']) ? $arys['data']['item_id'] : 0;
        $itemOne = (array) self::dbConn(self::$dbConfigItem)
            ->where('id', $arys['data']['item_id'])
            ->first();
        $arys['data']['item_name'] = empty($itemOne) ? '-' : $itemOne['title'];
        return self::toback(0, '', $arys);
    }

    // 任务列表
    public static function getTaskList($in_day, $depart_id)
    {
        $db = self::dbConn(self::$dbYwTask)
            // ->select('id', 'title')
            ->where('del', 0);
        if ($in_day) {
            $db = $db->where('in_day', $in_day);
        }
        if ($depart_id) {
            $db = $db->where('depart_id', $depart_id);
        }
        $res = $db->get();
        $arys = [];
        if (!count($res)) {
            return $arys;
        }
        foreach ($res as $v) {
            $departOne = (array) self::dbConn(self::$dbStDepart)
                ->where('id', $v->depart_id)
                ->first();
            $depart_name = empty($departOne) ? '' : $departOne['title'];
            $arys[] = [
                'id' => $v->id, 'title' => $v->title,
                'in_day' => $v->in_day, 'depart_name' => $depart_name,
            ];
        }
        return $arys;
    }

    //通过UID获取项目列表
    public static function getItemListByUid($uid, $depart_id)
    {
        $uOne = (array) self::dbConn(self::$dbStaff)->where('id', $uid)->first();
        if (empty($uOne)) {
            return [];
        }
        if ($uOne['depart_lead'] == 2) {
            return self::dbConn(self::$dbConfigItem)
                ->select('id', 'title')
                ->where('del', 0)
                ->where('depart_id', $depart_id)
                ->where('is_able', 1)
                ->get();
        }
        $uis = self::dbConn(self::$dbConfigTeam)
            ->where('del', 0)
            ->where('staff_id', $uid)
            ->get();
        $itemids = [];
        if (!count($uis)) {
            return [];
        }
        foreach ($uis as $v) {
            $itemids[] = $v->item_id;
        }
        if (!count($itemids)) {
            return [];
        }
        return self::dbConn(self::$dbConfigItem)
            ->select('id', 'title')
            ->where('del', 0)
            ->where('depart_id', $depart_id)
            ->whereIn('id', $itemids)
            ->get();
    }

    //设置工时
    public static function setByItem($id, $pas)
    {
        $pas['uptime'] = toDate();
        $res = self::dbConn(self::$dbYwWh)->where('id', $id)->first();
        if (!empty($res)) {
            //return self::toback();
            self::dbConn(self::$dbYwWh)->where('id', $id)->update($pas);
            $lastid = $id;
        } else {
            $pas['addtime'] = toDate();
            $lastid = self::dbConn(self::$dbYwWh)->insertGetId($pas);
            self::dbConn(self::$dbYwWhStatus)->insertGetId([
                'wh_id' => $lastid, 'pub_uid' => $pas['staff_id'], 'addtime' => toDate(),
            ]);
        }
        return self::toback(0, '', $lastid);
    }

    //保存
    public static function modify($id, $pas)
    {
        $res = (array) self::dbConn(self::$dbYwWh)->where('id', $id)->first();
        if (empty($res)) {
            return self::toback(-2, '没有数据');
        }
        $pas['status'] = 2;
        $pas['uptime'] = toDate();
        self::dbConn(self::$dbYwWh)->where('id', $id)->update($pas);
        self::dbConn(self::$dbYwWhStatus)->insertGetId([
            'wh_id' => $res['id'], 'pub_uid' => $res['staff_id'],
            'type' => 2, 'addtime' => toDate(),
        ]);
        return self::toback();
    }

    //发布
    public static function pub($id)
    {
        $res = (array) self::dbConn(self::$dbYwWh)->where('id', $id)->first();
        if (empty($res)) {
            return self::toback(-2, '没有数据');
        }
        self::dbConn(self::$dbYwWh)->where('id', $id)->update([
            'status' => 3, 'uptime' => toDate(),
        ]);
        self::dbConn(self::$dbYwWhStatus)->insertGetId([
            'wh_id' => $res['id'], 'pub_uid' => $res['staff_id'],
            'type' => 3, 'addtime' => toDate(),
        ]);
        return self::toback();
    }

    //设置为默认
    public static function itemDefault($uid, $item_id)
    {
        $res = self::dbConn(self::$dbConfigWhUid)
            ->where('del', 0)
            ->where('uid', $uid)
            ->where('item_id', $item_id)
            ->first();
        if (!empty($res)) {
            return self::toback(-2, '已设置为默认');
        }
        self::dbConn(self::$dbConfigWhUid)
            ->where('del', 0)
            ->where('uid', $uid)
            ->where('item_id', '<>', $item_id)
            ->update(['del' => 1, 'deltime' => toDate()]);
        self::dbConn(self::$dbConfigWhUid)->insertGetId([
            'uid' => $uid, 'item_id' => $item_id, 'addtime' => toDate(),
        ]);
        return self::toback();
    }

    //删除
    public static function del($id)
    {
        $res = (array) self::dbConn(self::$dbYwWh)->where('id', $id)->first();
        if (empty($res)) {
            return self::toback(-2, '数据不存在');
        }
        if (in_array($res['status'], [3,5,6,7])) {
            return self::toback(-2, '已提交，不可删除');
        }
        self::dbConn(self::$dbYwWh)
            ->where('id', $id)
            ->update(['del' => 1, 'deltime' => toDate()]);
        self::dbConn(self::$dbYwWhItem)
            ->where('wh_id', $id)
            ->update(['del' => 1, 'deltime' => toDate()]);
        return self::toback();
    }

    //审核列表
    public static function authIndex($pages, $pas)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = self::dbConn(self::$dbYwWh.' as a')
            ->select('a.*')
            ->leftJoin(self::$dbStaff.' as b', 'a.staff_id', '=', 'b.id')
            ->where('a.del', 0);
        // $db = $db->where('in_day', date('Y-m-d', time()));
        $db = $db->where('a.status', 3);
        if ($pas['key']) {
            $db = $db->where('b.title', 'like', '%'.$pas['key'].'%');
        }
        if ($pas['sort_id'] == 2) {
            $db = $db->orderBy('a.in_day', 'desc');
        } else {
            $db = $db->orderBy('a.in_day', 'asc');
        }
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])->get();
        if (!count($res)) {
            return self::toback(-2, '数据不存在');
        }
        $types = self::$whStatuss;
        $arys = [];
        foreach ($res as $v) {
            $itemOne = (array) self::dbConn(self::$dbConfigItem)
                ->where('id', $v->item_id)
                ->first();
            $item_name = empty($itemOne) ? '-' : $itemOne['title'];
            $taskOne = (array) self::dbConn(self::$dbYwTask)
                ->where('id', $v->task_id)
                ->first();
            $task_name = empty($taskOne) ? '-' : $taskOne['title'];
            $staffOne = (array) self::dbConn(self::$dbStaff)
                ->where('id', $v->staff_id)
                ->first();
            $staff_name = empty($staffOne) ? '-' : $staffOne['title'];
            $statusOne = (array) self::dbConn(self::$dbYwWhStatus)
                ->where('wh_id', $v->id)
                ->orderBy('id', 'desc')
                ->first();
            $status_id = empty($statusOne) ? '-' : $statusOne['type'];
            $status_name = array_key_exists($status_id, $types) ? $types[$status_id] : '-';
            $itemOnes = self::dbConn(self::$dbYwWhItem)
                ->where('del', 0)
                ->where('wh_id', $v->id)
                ->orderBy('starthour', 'asc')
                ->get();
            $whList = [];
            $delay_day = 0;
            if (count($itemOnes)) {
                foreach ($itemOnes as $vv) {
                    $cfgOne = (array) self::dbConn(self::$dbConfigWorkHour)
                        ->where('id', $vv->config_whid)
                        ->first();
                    $cfg_name = empty($cfgOne) ? '' : $cfgOne['title'];
                    $whList[] = [
                        'id' => $vv->id, 'title' => $cfg_name,
                        'starthour' => $vv->starthour, 'endhour' => $vv->endhour,
                        'delay' => $vv->delay, 
                        'up_day' => explode(' ', $vv->uptime)[0],
                    ];
                    $delay_day += $vv->delay;
                }
            }
            // $in_day = date('Y-m-d', strtotime($v->uptime));
            $day_shi = floor($delay_day / 60);
            $day_fen = $delay_day - $day_shi * 60;
            // ddd($delay_day, $day_shi, $day_fen);
            $day_str = $day_shi . '小时' . $day_fen . '分钟';
            $arys[] = [
                'id' => $v->id,
                'item_id' => $v->item_id, 'item_name' => $item_name,
                'task_id' => $v->task_id, 'task_name' => $task_name,
                'staff' => $v->staff_id, 'staff_name' => $staff_name,
                'status_id' => $status_id, 'status_name' => $status_name,
                'whList' => $whList, 'lasttime' => $v->uptime,
                'in_day' => $v->in_day, 'delay_str' => $day_str,
            ];
        }
        return self::toback(0, '', $arys, $total);
    }

    //通过列表
    public static function okIndex($pages, $pas)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = self::dbConn(self::$dbYwWh.' as a')
            ->select('a.*')
            ->leftJoin(self::$dbStaff.' as b', 'a.staff_id', '=', 'b.id')
            ->where('del', 0);
        $db = $db->where('status', '>', 4);
        if ($pas['key']) {
            $db = $db->where('b.title', 'like', '%'.$pas['key'].'%');
        }
        if ($pas['sort_id'] == 2) {
            $db = $db->orderBy('a.in_day', 'desc');
        } else {
            $db = $db->orderBy('a.in_day', 'asc');
        }
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])->get();
        if (!count($res)) {
            return self::toback(-2, '数据不存在');
        }
        $types = self::$whStatuss;
        $arys = [];
        foreach ($res as $v) {
            $itemOne = (array) self::dbConn(self::$dbConfigItem)
                ->where('id', $v->item_id)
                ->first();
            $item_name = empty($itemOne) ? '-' : $itemOne['title'];
            $taskOne = (array) self::dbConn(self::$dbYwTask)
                ->where('id', $v->task_id)
                ->first();
            $task_name = empty($taskOne) ? '-' : $taskOne['title'];
            $staffOne = (array) self::dbConn(self::$dbStaff)
                ->where('id', $v->staff_id)
                ->first();
            $staff_name = empty($staffOne) ? '-' : $staffOne['title'];
            $statusOne = (array) self::dbConn(self::$dbYwWhStatus)
                ->where('wh_id', $v->id)
                ->orderBy('id', 'desc')
                ->first();
            $status_id = empty($statusOne) ? '-' : $statusOne['type'];
            $status_name = array_key_exists($status_id, $types) ? $types[$status_id] : '-';
            $itemOnes = self::dbConn(self::$dbYwWhItem)
                ->where('del', 0)
                ->where('wh_id', $v->id)
                ->orderBy('starthour', 'asc')
                ->get();
            $whList = [];
            $delay_total = 0;
            if (count($itemOnes)) {
                foreach ($itemOnes as $vv) {
                    $cfgOne = (array) self::dbConn(self::$dbConfigWorkHour)
                        ->where('id', $vv->config_whid)
                        ->first();
                    $cfg_name = empty($cfgOne) ? '' : $cfgOne['title'];
                    $whList[] = [
                        'id' => $vv->id, 'title' => $cfg_name,
                        'starthour' => $vv->starthour, 'endhour' => $vv->endhour,
                        'delay' => $vv->delay,
                        'up_day' => explode(' ', $vv->uptime)[0],
                    ];
                    $delay_total += $vv->delay;
                }
            }
            // $in_day = date('Y-m-d', strtotime($v->uptime));
            $day_shi = floor($delay_total / 60);
            $day_fen = $delay_total - $day_shi * 60;
            $day_str = $day_shi . '小时' . $day_fen . '分钟';
            $arys[] = [
                'id' => $v->id,
                'item_id' => $v->item_id, 'item_name' => $item_name,
                'task_id' => $v->task_id, 'task_name' => $task_name,
                'staff' => $v->staff_id, 'staff_name' => $staff_name,
                'status_id' => $status_id, 'status_name' => $status_name,
                'whList' => $whList, 'lasttime' => $v->uptime,
                'in_day' => $v->in_day,
                'delay_str' => $day_str,
            ];
        }
        return self::toback(0, '', $arys, $total);
    }

    //审核通过
    public static function authOk($id)
    {
        $res = self::dbConn(self::$dbYwWh)->where('id', $id)->first();
        if (empty($res)) {
            return self::toback(-2, '数据不存在');
        }
        $res = self::dbConn(self::$dbYwWh)
            ->where('id', $id)
            ->update(['status' => 5]);
        // if (!$res) {
        //     return self::toback(-1, 'error');
        // }
        $res = self::dbConn(self::$dbYwWhStatus)->insertGetId([
            'wh_id' => $id, 'type' => 5, 'addtime' => toDate(),
        ]);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

    //审核拒绝
    public static function authNo($id, $remark)
    {
        $res = self::dbConn(self::$dbYwWh)->where('id', $id)->first();
        if (empty($res)) {
            return self::toback(-2, '数据不存在');
        }
        $res = self::dbConn(self::$dbYwWh)
            ->where('id', $id)
            ->update(['status' => 4]);
        // if (!$res) {
        //     return self::toback(-1, 'error');
        // }
        $res = self::dbConn(self::$dbYwWhStatus)->insertGetId([
            'wh_id' => $id, 'type' => 4, 'remark' => $remark, 'addtime' => toDate(),
        ]);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

    //详情
    public static function getRefuseOne($id)
    {
        $res = (array) self::dbConn(self::$dbYwWh)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return self::toback(-2, '没有数据');
        }
        //录入日期
        $in_day = isset($res['in_day'])&&$res['in_day'] ? $res['in_day'] : date('Y-m-d', time());
        $res['in_day'] = $in_day;
        $itemOne = (array) self::dbConn(self::$dbConfigItem)
            ->where('id', $res['item_id'])
            ->first();
        $taskOne = (array) self::dbConn(self::$dbYwTask)
            ->where('id', $res['task_id'])
            ->first();
        $res['item_name'] = empty($itemOne) ? '' : $itemOne['title'];
        $res['task_name'] = empty($taskOne) ? '' : $taskOne['title'];
        $itemOnes = self::dbConn(self::$dbYwWhItem)
            ->where('del', 0)
            ->where('wh_id', $res['id'])
            ->orderBy('starthour', 'asc')
            ->groupBy('config_whid')
            ->get();
        $whList = [];
        $delay_total = 0;
        if (count($itemOnes)) {
            foreach ($itemOnes as $v) {
                $configOne = (array) self::dbConn(self::$dbConfigWorkHour)
                    ->where('id', $v->config_whid)
                    ->first();
                $title = empty($configOne) ? '' : $configOne['title'];
                $subOnes = self::dbConn(self::$dbYwWhItem)
                    ->where('del', 0)
                    ->where('config_whid', $v->config_whid)
                    ->where('wh_id', $id)
                    ->orderBy('starthour', 'asc')
                    ->get();
                $subList = [];
                $delay_pid = 0;
                if (count($subOnes)) {
                    foreach ($subOnes as $vv) {
                        $cntOne = (array) self::dbConn(self::$dbConfigWhCnt)
                            ->where('id', $vv->cnt_id)
                            ->first();
                        $resDelay = self::getDelay($vv->starthour, $vv->endhour);
                        $subList[] = [
                            'id' => $vv->id,
                            'cnt_id' => $v->cnt_id,
                            'cnt_str' => empty($cntOne) ? '' : $cntOne['cnt'],
                            'starthour' => $vv->starthour,
                            'endhour' => $vv->endhour,
                            'delay' => $vv->delay,
                            'delay_str' => $resDelay['code'] < 0 ? '' : $resDelay['data']['str'],
                        ];
                        $delay_pid += $vv->delay;
                    }
                }
                $whList[] = [
                    'id' => $v->config_whid, 'title' => $title,
                    'config_whid' => $v->config_whid,
                    'config_title' => $title,
                    'child' => $subList,
                    'delay' => self::shiByFen($delay_pid),
                ];
                $delay_total += $delay_pid;
            }
        }
        $res['whList'] = $whList;
        $res['delay'] = self::shiByFen($delay_total);
        return self::toback(0, '', $res);
    }

    // 清理旧数据
    public static function clearData()
    {
        $res = self::dbConn(self::$dbYwWh)
            ->where('del', 1)
            ->get();
        if (!count($res)) return false;
        $ids = [];
        foreach ($res as $v) {
            $ids[] = $v->id;
        }
        self::dbConn(self::$dbYwWhItem)
            ->where('del', 0)
            ->whereIn('wh_id', $ids)
            ->update(['del' => 1, 'deltime' => toDate()]);
        return true;
    }
}